<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Tourpoule
*
* Author: John Schop, john.schop@gmail.com
*
*/

class Tourpoule
{

  public function __construct()
  {

  }


  public function calculate_stage($stage_id)
  {
    $time_start = microtime(true);
    $CI =& get_instance();

    // Get Stage Results
    $CI->load->model('stages_model');
    $stage = $CI->stages_model->get_by_id($stage_id);

    // Get the team selections for all active users
    $CI->load->model('selections_model');
    $selections = $CI->selections_model->get_all_active();

    // Get the points, this needs to be configurable
    $CI->load->model('config_model');

    if ($stage_id != 20)
    {
      $points_stage           = explode(',', $CI->config_model->item('points_stage'));
      $points_stage_yellow    = explode(',', $CI->config_model->item('points_stage_yellow'));
      $points_stage_green     = explode(',', $CI->config_model->item('points_stage_green'));
      $points_stage_mountain  = explode(',', $CI->config_model->item('points_stage_mountain'));
    }
    else
    {
      $points_stage           = explode(',', $CI->config_model->item('points_stage'));
      $points_stage_yellow    = explode(',', $CI->config_model->item('points_end_yellow'));
      $points_stage_green     = explode(',', $CI->config_model->item('points_end_green'));
      $points_stage_mountain  = explode(',', $CI->config_model->item('points_end_mountain'));     
    }

    $stage_result = explode(',', $stage->stage_result);
    $stage_yellow = explode(',', $stage->stage_yellow);
    $stage_green = explode(',', $stage->stage_green);
    $stage_mountain = explode(',', $stage->stage_mountain);

    // check riders
    $CI->load->model('riders_model');

    foreach($stage_result as $pos=>$rider_id)
    {
      $rider = $CI->riders_model->get_by_id($rider_id);
      if(is_object($rider))
        {
          $rider_points = explode(',',$rider->points_stage);
          $rider_points[$stage_id] = $points_stage[$pos];
          $CI->riders_model->update($rider_id, array('points_stage'=>implode(',',$rider_points)));
        }
    }

    foreach($stage_yellow as $pos=>$rider_id)
    {
      $rider = $CI->riders_model->get_by_id($rider_id);
      if(is_object($rider))
        {
          $rider_points = explode(',',$rider->points_stage_yellow);
          $rider_points[$stage_id] = $points_stage_yellow[$pos];
          $CI->riders_model->update($rider_id, array('points_stage_yellow'=>implode(',',$rider_points)));
        }
    }

    foreach($stage_green as $pos=>$rider_id)
    {
      $rider = $CI->riders_model->get_by_id($rider_id);
      if(is_object($rider))
        {
          $rider_points = explode(',',$rider->points_stage_green);
          $rider_points[$stage_id] = $points_stage_green[$pos];
          $CI->riders_model->update($rider_id, array('points_stage_green'=>implode(',',$rider_points)));
        }
    }

    foreach($stage_mountain as $pos=>$rider_id)
    {
      $rider = $CI->riders_model->get_by_id($rider_id);
      if(is_object($rider))
        {
          $rider_points = explode(',',$rider->points_stage_mountain);
          $rider_points[$stage_id] = $points_stage_mountain[$pos];
          $CI->riders_model->update($rider_id, array('points_stage_mountain'=>implode(',',$rider_points)));
        }
    }

    // check for each active user if the results match a rider in their selection
    foreach($selections as $user => $selection)
    {
      $user_points_stage = 0;
      foreach($stage_result as $pos => $rider_id)
      {
        if (in_array($rider_id, $selection)) // rider is in this users selection
        {
          $user_points_stage = $user_points_stage + $points_stage[$pos];
        }
      }

      $user_points_yellow = 0;
      foreach($stage_yellow as $pos => $rider_id)
      {
        if (in_array($rider_id, $selection)) // rider is in this users selection
        {
          $user_points_yellow = $user_points_yellow + $points_stage_yellow[$pos];
        }        
      }

      $user_points_green = 0;
      foreach($stage_green as $pos => $rider_id)
      {
        if (in_array($rider_id, $selection)) // rider is in this users selection
        {
          $user_points_green = $user_points_green + $points_stage_green[$pos];
        }        
      }

      $user_points_mountain = 0;
      foreach($stage_mountain as $pos => $rider_id)
      {
        if (in_array($rider_id, $selection)) // rider is in this users selection
        {
          $user_points_mountain = $user_points_mountain + $points_stage_mountain[$pos];
        }        
      }
      $user_points_total = $user_points_stage + $user_points_yellow + $user_points_green + $user_points_mountain;
      $CI->load->model('points_model');
      $CI->points_model->update($user, $stage_id, array('points_stage'    => $user_points_stage,
                                                        'points_yellow'   => $user_points_yellow,
                                                        'points_green'    => $user_points_green,
                                                        'points_mountain' => $user_points_mountain,
                                                        'points_total'    => $user_points_total
                                                        )
                                );


    }

    // get the top 10 point totals for this stage
    $data['top_10'] = $CI->points_model->get_top_10($stage_id);

    // Check if we need to send e-mails
    if ($CI->config_model->item('send_email_to_top_ten'))
    {
      $CI->load->library('email');
      $config_email['protocol'] = 'sendmail';
      $CI->email->initialize($config_email);

      foreach($data['top_10'] as $user)
      {
        //send the e-mail

        $CI->email->from($CI->config->item('admin_email', 'ion_auth'), $CI->config->item('site_title', 'ion_auth'));
        $CI->email->to($user->email);
        $CI->email->bcc($CI->config->item('admin_email', 'ion_auth'));

        $CI->email->subject('Top 10!');
        $CI->email->message('Gefeliciteerd, je haalde vandaag de Top 10! Je haalde '.$user->points_total.' punten in de etappe van vandaag.');

        $CI->email->send();
      }
    }

    // set stage calculated
    $CI->stages_model->update($stage_id, array('calculated'=>1));

    $time_end = microtime(true);
    $data['counter'] = count($selections);
    $data['time_elapsed'] = $time_end - $time_start;
    return $data;
  }

}